//
//  TZEditTextView.m
//  SelectPhotosOrVideoContentViewLib
//
//  Created by Dubaoquan on 2019/10/25.
//

#import "TZEditTextView.h"
#import "AppConst.h"

@interface TZEditTextView ()<UIGestureRecognizerDelegate>

@end
@implementation TZEditTextView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
  
        [self setup];
        
    }
    return self;
}

- (void)setup {
    self.scrollEnabled = NO;
    self.scrollsToTop = NO;
    self.selectable = NO;
    self.showsHorizontalScrollIndicator = NO;
    self.enablesReturnKeyAutomatically = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidEndEditing) name:UITextViewTextDidEndEditingNotification object:self];
    
    NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle alloc] init];
    textStyle.lineBreakMode = NSLineBreakByWordWrapping;
    textStyle.alignment = NSTextAlignmentCenter;
    textStyle.lineSpacing = 5;
    textStyle.firstLineHeadIndent = 0.0;
    textStyle.headIndent = 0.0;
    textStyle.tailIndent = 0.0;
    textStyle.minimumLineHeight = 10.0;
    textStyle.maximumLineHeight = self.font.lineHeight;
    textStyle.paragraphSpacing = 0;
    textStyle.paragraphSpacingBefore = 0.0f;
    textStyle.baseWritingDirection = NSWritingDirectionLeftToRight;
    textStyle.lineHeightMultiple = 1.0;
    textStyle.hyphenationFactor = 1;
    
    NSMutableDictionary *textAttributes = [[NSMutableDictionary alloc] init];
    
    [textAttributes setValue:textStyle forKey:NSParagraphStyleAttributeName];
    
    [textAttributes setValue:self.font forKey:NSFontAttributeName];
    
    [textAttributes setValue:self.textColor forKey:NSForegroundColorAttributeName];
    
    self.typingAttributes = textAttributes;
//    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress)];
//    longPress.delegate=self;
//
//    [self addGestureRecognizer:longPress];
    
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*) otherGestureRecognizer{
    return NO;
//    if([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]){
//        return NO;
//    }
//    return YES;
}

-(void)textDidEndEditing{
    
    
}
-(void)longPress{
    self.editable = YES;
    [self becomeFirstResponder];
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{

    UITouch *touch = [touches anyObject];
    
    CGPoint currentP = [touch locationInView:self];
    CGPoint previousP = [touch previousLocationInView:self];
    CGFloat offsetX = currentP.x - previousP.x;
    CGFloat offsetY = currentP.y - previousP.y;

    self.transform = CGAffineTransformTranslate(self.transform, offsetX, offsetY);
}

- (void)textDidChange {
    

//    NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle alloc] init];
//    textStyle.lineBreakMode = NSLineBreakByWordWrapping;
//    textStyle.alignment = NSTextAlignmentCenter;
//    textStyle.lineSpacing = 5;
//    textStyle.firstLineHeadIndent = 0.0;
//    textStyle.headIndent = 0.0;
//    textStyle.tailIndent = 0.0;
//    textStyle.minimumLineHeight = 10.0;
//    textStyle.maximumLineHeight = self.font.lineHeight;
//    textStyle.paragraphSpacing = 0;
//    textStyle.paragraphSpacingBefore = 0.0f;
//    textStyle.baseWritingDirection = NSWritingDirectionLeftToRight;
//    textStyle.lineHeightMultiple = 1.0;
//    textStyle.hyphenationFactor = 1;
//    
//    NSMutableDictionary *textAttributes = [[NSMutableDictionary alloc] init];
//    
//    [textAttributes setValue:textStyle forKey:NSParagraphStyleAttributeName];
//    
//    [textAttributes setValue:self.font forKey:NSFontAttributeName];
//    
//    [textAttributes setValue:self.textColor forKey:NSForegroundColorAttributeName];
//    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:self.text];
//    [attributedText setAttributes:textAttributes range:NSMakeRange(0, self.text.length)];
//    
//    self.attributedText = attributedText;

    
    CGFloat maxWidth = self.superview.bounds.size.width-2*kEdgeIntegral;
    CGFloat currentLineWidth = [self sizeThatFits:CGSizeMake(MAXFLOAT, self.font.lineHeight+self.textContainerInset.top+self.textContainerInset.bottom)].width;//ceilf([self.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, self.font.lineHeight+self.textContainerInset.top+self.textContainerInset.bottom) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:textAttributes context:nil].size.width);
    
    if (currentLineWidth>maxWidth) {
        CGFloat height = ceilf([self sizeThatFits:CGSizeMake(maxWidth, MAXFLOAT)].height);
        self.frame = CGRectMake((self.superview.bounds.size.width-maxWidth)/2.0, self.frame.origin.y, maxWidth, height);
        
    }else{
        self.frame = CGRectMake((self.superview.bounds.size.width-currentLineWidth)/2.0, self.frame.origin.y, currentLineWidth, self.font.lineHeight+self.textContainerInset.top+self.textContainerInset.bottom);
    }
    
    if (self.textChangedBlock) {
        self.textChangedBlock(self.frame);
    }
    self.layer.cornerRadius = CGRectGetHeight(self.frame)/10.0>20?20:(CGRectGetHeight(self.frame)/10.0);
    self.layer.masksToBounds = YES;
    [self layoutIfNeeded];
}
-(CGSize)sizeWithString:(NSString *)string font:(UIFont *)font constraintSize:(CGSize)constraintSize
{
    CGSize stringSize = CGSizeZero;

    NSDictionary *attributes = @{NSFontAttributeName:font};
    NSInteger options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    CGRect stringRect = [string boundingRectWithSize:constraintSize options:options attributes:attributes context:NULL];
    stringSize = stringRect.size;

    return stringSize;
}
-(void)startEdite{
    
    self.editable = YES;
    [self becomeFirstResponder];
 
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
